/**
 * 
 * 
 *  
 *====================================================
 * 文件名称: MyEncryptor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2010-1-22			
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
package com.devframe.sys.encrypt;

import java.io.UnsupportedEncodingException;

/**
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;MyEncryptor&lt;/font&gt;&lt;/b&gt;
 * </pre>
 * 
 * <pre>
 * &lt;b&gt; --支持密钥的加密解密算法--&lt;/b&gt;
 * </pre>
 * <pre>支持全角字符加密，即支持数字、字母、符号、汉字等的加密。</pre>
 * <pre>
 * &lt;b&gt;--样例--&lt;/b&gt;
 *   MyEncryptor obj = new MyEncryptor();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * 
 */
public class MyEncryptor implements Encryptor{
    /**
     * 加密密钥
     */
    private String encKey = "MyCreation";;
    public void setEncrypt(String key){
        this.encKey=key;
    }
    /**
     * 加密
     * @param value 待加密值
     * @return 加密结果
     * 
     */
    public String encrypt(String value) {
        String result = "";
        if ((value != null) && (!(value.equals("")))) {
            byte[] valueBytes;
            try {
               //valueBytes = value.getBytes();
               //改统一用UTF-8，避免不同环境字符集不同导致的问题
                valueBytes = value.getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                valueBytes = value.getBytes();
            }
            int len1 = valueBytes.length / encKey.length();
            String longKey = "";
            for (int i = 0; i <= len1; ++i) {
                longKey = longKey + encKey;
            }
            byte[] longKeyBytes = longKey.getBytes();
            for (int i = 0; i < valueBytes.length; ++i) {
                byte b1 = valueBytes[i];
                byte b2 = longKeyBytes[i];
                String lpResultStr = Integer.toHexString((b1 ^ b2) & 0xFF).toUpperCase();
                if (lpResultStr.length() < 2) {
                    lpResultStr = "0" + lpResultStr;
                }
                result = result + lpResultStr;
            }
        }
        return result;
    }

    /**
     * 解密
     * @param value 待解密值
     * @return 解密结果
     * 
     */
    public String decrypt(String value) {
        String result = "";
        try {
            if ((value != null) && (!(value.equals("")))) {
                int len1 = value.length() / encKey.length();
                String longKey = "";
                for (int i = 0; i <= len1; ++i) {
                    longKey = longKey + encKey;
                }
                byte[] longKeyBytes = longKey.getBytes();
                byte[] encValueBytes = new byte[value.length() / 2];

                for (int i = 0; i < encValueBytes.length; ++i) {
                    String lpResultStr = value.substring(i * 2, i * 2 + 2);
                    encValueBytes[i] = (byte) Integer.parseInt(lpResultStr, 16);
                }
                byte[] lpResultBytes = new byte[encValueBytes.length];
                for (int i = 0; i < encValueBytes.length; ++i) {
                    byte b1 = encValueBytes[i];
                    byte b2 = longKeyBytes[i];
                    lpResultBytes[i] = (byte) (b1 ^ b2);
                }
                //result = new String(lpResultBytes);
                //统一用utf-8字符集 
                result = new String(lpResultBytes,"utf-8");
            }
        } catch (Exception e) {
            result = value;
        }
        return result;
    }

    /**
     * testEncDec 测试加密和解密
     * @param value
     *            测试值
     * 
     */
    private void testEncDec(String value) {
        String encResult = encrypt(value);
        String decResult = decrypt(encResult);
        System.out.println(value + "->" + encResult + "->" + decResult);
    }

    /**
     * main
     * @param args
     *            参数
     * 
     */
    public static void main(String[] args) {
        try {
            MyEncryptor test = new MyEncryptor();
            test.testEncDec("厦门市工程有限公司");
            test.testEncDec("信息资源管理系统");
            test.testEncDec("123456");
            test.testEncDec("（）+-【】\\[]'");
            test.testEncDec("abcde");
            test.testEncDec("ABCDE");
            test.testEncDec("EDCBA");
            test.testEncDec("A    A");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }    
}
